package com.juzipi.demo.web.admin;


import com.juzipi.demo.pojo.Blog;
import com.juzipi.demo.pojo.User;
import com.juzipi.demo.service.BlogService;
import com.juzipi.demo.service.TagService;
import com.juzipi.demo.service.TypeService;
import com.juzipi.demo.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;



    private static final String INPUT = "admin/blogs-input";
    private static final String LIST = "admin/blogs";
    private static final String REDIRECT_LIST = "redirect:/admin/blogs";


    @GetMapping("/blogs")
    public String list(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, Blog blog, BlogQuery blogQuery,Model model) {
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page",blogService.listBlog(pageable,blogQuery));
        return LIST;

    }


    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, Blog blog,BlogQuery blogQuery, Model model) {

        model.addAttribute("page",blogService.listBlog(pageable,blogQuery));
        return "admin/blogs :: blogList";
    }


    @GetMapping("/blogs/input")
    public String input(Model model) {
//        model.addAttribute("types",typeService.listType());
//        model.addAttribute("tags",tagService.listTag());
        setTypeAndTag(model);
        model.addAttribute("blog",new Blog());
        return INPUT;
    }


    private void setTypeAndTag(Model model) {
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
    }


    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model) {
//        model.addAttribute("types",typeService.listType());
//        model.addAttribute("tags",tagService.listTag());
        setTypeAndTag(model);

        Blog blog = blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog",blog);
        return INPUT;
    }



    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes redirectAttributes, HttpSession httpSession) {
        blog.setUser((User) httpSession.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        Blog saveBlog;
        if (StringUtils.isEmpty(blog.getId())){
            saveBlog = blogService.saveBlog(blog);
        }else {
            saveBlog = blogService.updateBlog(blog.getId(),blog);
        }
        if (StringUtils.isEmpty(saveBlog)){
            redirectAttributes.addFlashAttribute("message","operation failed!");
        }
        redirectAttributes.addFlashAttribute("message","Successful operation!");
        return REDIRECT_LIST;
    }



    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes redirectAttributes) {
        blogService.deleteBlog(id);
        redirectAttributes.addFlashAttribute("message","operation failed!");
        return REDIRECT_LIST;
    }

}
